#include "sensorvalue_generation.h"

/**
 * Returns random number between upper and lower value.
 * Both should be positive, with upper higher than lower.
 */
double randBetween(double upper, double lower) {
	return ((rand() / (double) RAND_MAX) * (upper - lower)) + lower;
}

//double generateAnalogValueOld() {
//	double randomNumber = rand() / (double) RAND_MAX; // Random number between 0 and 1
//	return ANALOG_CENTRE_VALUE - (ANALOG_MAX_DEVIATION) + (randomNumber * ANALOG_MAX_DEVIATION * 2);
//}

double generateAnalogValue(struct AnalogSensor *sensor) {
	double offset = (sensor->upperLimit - sensor->lowerLimit) / SENSORVALUE_GENERATION_OFFSET_DIVIDER;
	return randBetween(sensor->upperLimit + offset, sensor->lowerLimit - offset);
}

/**
 * Gets a random unit for a sensor
 */
void getRandomUnit(struct AnalogSensor* sensor) {
	int unitIndex = randBetween(0, 8);
	switch(unitIndex) {
	case 0:
		strcpy(sensor->unit, "Pascal");
		break;
	case 1:
		strcpy(sensor->unit, "Bar");
		break;
	case 2:
		strcpy(sensor->unit, "millimetres");
		break;
	case 3:
		strcpy(sensor->unit, "Kg");
		break;
	case 4:
		strcpy(sensor->unit, "seconds");
		break;
	case 5:
		strcpy(sensor->unit, "Ampere");
		break;
	case 6:
		strcpy(sensor->unit, "Kelvin");
		break;
	case 7:
		strcpy(sensor->unit, "Candela");
		break;
	case 8:
		strcpy(sensor->unit, "Sv/s");
		break;
	}
}

/**
 * Returns a 1 or 0.
 * 1 represents "true", which means alarm value, 0 is OK.
 */
int generateBinaryValue() {
	double randomNumber = rand() / (double) RAND_MAX; // Random number between 0 and 1
	double chanceNumber = 1.0 / BINARY_SENSOR_CHANCE_FOR_TRUE;
	if (randomNumber < chanceNumber) {
		return 1;
	} else {
		return 0;
	}
}
